package com.elonnx.web;

import com.elonnx.service.UserService;
import jdk.nashorn.internal.ir.RuntimeNode;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 用户Servlet
 * @author 马元
 * @date 2024-07-11
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1.接收表单参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        // 2.业务逻辑处理
        // 查询数据库
        UserService userService = new UserService();
        boolean flag = userService.login(username, password);

        // 3.页面跳转
        if (flag) {
            // 返回成功页面
            //request.getRequestDispatcher("/success.jsp").forward(request, response);
            response.sendRedirect(request.getContextPath() + "/student/list");
        } else {
            // 返回失败页面
            request.getRequestDispatcher("/failed.jsp").forward(request, response);
        }

    }

}
